package com.project.estate.chargeitem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.project.estate.chargeitem.service.ChargeItemService;
import com.project.estate.commons.bean.ChargeItem;
import com.project.estate.commons.config.JsonResult;
import com.project.estate.commons.config.ResultTool;
import com.project.estate.commons.handler.ChargeItemAddException;
import com.project.estate.commons.handler.ChargeItemDeleteException;
import com.project.estate.commons.handler.ChargeItemFindException;
import com.project.estate.commons.mapper.ChargeItemMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class ChargeItemServiceImpl extends ServiceImpl<ChargeItemMapper, ChargeItem> implements ChargeItemService {


    @Resource
    private ChargeItemMapper chargeItemMapper;

    //添加
    @Override
    public JsonResult addChargeItem(ChargeItem chargeItem) {
        boolean save = save(chargeItem);
        if (!save) {
            throw new ChargeItemAddException("添加失败");
        }
        return ResultTool.success("success");
    }

    //修改
    @Override
    public JsonResult updateChargeItemById(String com_id, ChargeItem chargeItem) {
        UpdateWrapper<ChargeItem> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("community_id", com_id);
        updateWrapper.set("community_name", chargeItem.getCommunityName());
        updateWrapper.set("code", chargeItem.getCode());
        updateWrapper.set("name", chargeItem.getName());
        updateWrapper.set("money", chargeItem.getMoney());
        boolean update = update(updateWrapper);
        if (!update) {
            throw new ChargeItemAddException("修改失败");
        }
        return ResultTool.success("success");
    }

    //删除
    @Override
    public JsonResult deleteChargeItemById(String com_id, ChargeItem chargeItem) {
        UpdateWrapper<ChargeItem> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("community_id", com_id);
        updateWrapper.set("delete_status", 1);
        boolean update = update(updateWrapper);
        if (!update) {
            throw new ChargeItemDeleteException("删除失败");
        }
        return ResultTool.success("success");
    }
    //查询
    @Override
    public JsonResult findChargeItemByChargeItemId(String com_id) {
        QueryWrapper<ChargeItem> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("community_id", com_id);
        List<ChargeItem> list = list(queryWrapper);
        if (list.isEmpty()) {
            throw new ChargeItemFindException("未查询到");
        }
        return ResultTool.success(list);
    }





}
